package com.aprivate.kn.ratingsystem.daoutil;

import android.content.Context;
import android.util.Log;

import com.aprivate.kn.ratingsystem.dao.DaoManager;
import com.aprivate.kn.ratingsystem.dao.ProductInfoDao;
import com.aprivate.kn.ratingsystem.daoBean.ProductInfo;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by kenan on 2018/11/29.
 */

public class ProductInfoDaoUtil {
    private DaoManager mManager;
    private String TAG="flag";
    //操作Fruit的构造函数
    public ProductInfoDaoUtil(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }
    /**
     * 完成user记录的插入，如果表未创建，先创建user表
     *
     * @param user
     * @return  返回long值 -1为失败
     */
    public boolean insertProductInfo(ProductInfo user) {
        boolean flag = false;
        flag = mManager.getDaoSession().getProductInfoDao().insertOrReplace(user) == -1 ? false : true;
        Log.i(TAG, "-------插入一条的结果为"+flag);
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     * @return
     */
    public boolean insertListUser(final List<ProductInfo> mList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (ProductInfo user : mList) {
                        mManager.getDaoSession().insertOrReplace(user);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     *   刷新指定数据
     */
    public boolean reFreshUser(ProductInfo user){
        boolean flag = false;
        mManager.getDaoSession().refresh(user);
        return flag;
    }
    /**
     * 修改一条数据
     * @return
     */
    public boolean updateUser(ProductInfo user){
        boolean flag = false;
        try {
            mManager.getDaoSession().update(user);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     * @return
     */
    public boolean deleteUser(ProductInfo user){
        boolean flag = false;
        try {
            mManager.getDaoSession().delete(user);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     * @return
     */
    public boolean deleteAll(){
        boolean flag = false;
        try {
            mManager.getDaoSession().deleteAll(ProductInfo.class);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<ProductInfo> queryAllUser(){
        return mManager.getDaoSession().loadAll(ProductInfo.class);
    }

    /**
     * 根据主键id查询记录
     * @param key
     * @return
     */
    public ProductInfo queryUserById(Long key){
        return mManager.getDaoSession().load(ProductInfo.class, key);
    }

    /**
     * 使用 sql语句进行查询操作
     *  参数一sql语句  参数二查询条件限定
     */
    public List<ProductInfo> queryUserBySql(String sql, String[] conditions){
        return mManager.getDaoSession().queryRaw(ProductInfo.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     * @return
     */
    public List<ProductInfo> queryUserByQueryBuilder(long id){
        QueryBuilder<ProductInfo> queryBuilder = mManager.getDaoSession().queryBuilder(ProductInfo.class);
        return queryBuilder.where(ProductInfoDao.Properties.Id.eq(id)).list();
    }

}
